﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class Data
    {
        SqlConnection con;
        private static string cs;

        public string Cs
        {
            get { return cs; }
            set { cs = value; }
        }
        public static string error;
        public Data()
        {
            //cs = "Data Source=DUCNHA;Initial Catalog=HUNGDONG2;Integrated Security=True";
            //con = new SqlConnection(cs);
        }
        public bool mo()
        {
            try
            {
                if (con == null)
                {
                    con = new SqlConnection(cs);
                }
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
                con.Open();
                return true;
            }
            catch (SqlException ex)
            {
                error = ex.ToString();
                return false;
            }
        }
       
        public bool ExecuteNonQuery(string procname, SqlParameter[] sqlparam)
        {
            try
            {
                if (mo())
                {
                    SqlCommand cmd = con.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = procname;
                    if (sqlparam != null)
                    {
                        cmd.Parameters.AddRange(sqlparam);
                    }
                    cmd.ExecuteNonQuery();
                    error = "";
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (SqlException ex)
            {
                error = ex.ToString();
                throw ex;// de cai nay day nha!
                //return false;
            }
            finally
            {
                con.Close();
            }
        }
        public DataTable ExecuteQueryNonParameter(string procname)
        {
            DataTable dt = new DataTable();
            try
            {
                if (mo())
                {
                    SqlCommand cmd = con.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = procname;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    error = "";
                    return dt;
                }
                else
                {
                    return null;
                }
            }
            catch (SqlException ex)
            {
                error = ex.ToString();
                throw ex;// de cai nay day nha!
                //return dt;
            }
            finally
            {
                con.Close();
            }
        }
        
        public DataTable ExecuteQueryWithSQLParameter(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                if (mo())
                {
                    SqlCommand cmd = con.CreateCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = sql;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    error = "";
                    return dt;
                }
                else
                {
                    return null;
                }
            }
            catch (SqlException ex)
            {
                error = ex.ToString();
                throw ex;// de cai nay day nha!
                //return dt;
            }
            finally
            {
                con.Close();
            }
        }

        public DataTable ExecuteQuery(string procname, SqlParameter[] sqlparam) 
        {
            DataTable dt = new DataTable();
            try
            {
                if (mo())
                {
                    SqlCommand cmd = con.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = procname;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    if (sqlparam != null)
                    {
                        cmd.Parameters.AddRange(sqlparam);
                    }
                    da.Fill(dt);
                    error = "";
                    return dt;
                }
                else
                {
                    return null;
                }
            }
            catch (SqlException ex)
            {
                error = ex.ToString();
                throw ex;// quang loi ra de ben ngoai bat. khong sua!
                //return null;
            }
            finally
            {
                con.Close();
            }
        }

        public bool ExecuteNonQuerytran(string procname, SqlParameter[] sqlparam, int a)
        {
            try
            {
                if (mo())
                {
                    SqlCommand cmd = con.CreateCommand();
                    cmd.Transaction = con.BeginTransaction();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = procname;
                    
                    if (sqlparam != null)
                    {
                        cmd.Parameters.AddRange(sqlparam);
                    }
                    cmd.ExecuteNonQuery();
                    error = "";
                    if (a == 0)
                    {
                        cmd.Transaction.Commit();
                    }
                    else
                    {
                        cmd.Transaction.Rollback();
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (SqlException ex)
            {
                error = ex.ToString();
                throw ex;// de cai nay day nha!
                //return false;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
